<?php
// +----------------------------------------------------------------------
// | TOPThink [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://topthink.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: luofei614 <weibo.com/luofei614>
// +----------------------------------------------------------------------
namespace Think\Upload\Driver;

use Think\Upload;
use Common\Util\Oss as OssClient;
// Oss环境文件写入存储类
class Oss{

    /**
     * 架构函数
     * @access public
     */
    private $error      =   '';
    private $client;
    private $contents   =   array();
    public function __construct() 
    {
        $this->client = OssClient::Client();
    }   

    /**
     * 检测上传根目录(OSS上传时支持自动创建目录，直接返回)
     * @param string $rootpath   根目录
     * @return boolean true-检测通过，false-检测失败
     */
    public function checkRootPath($rootpath){
        /* 设置根目录 */
        $this->rootPath = trim($rootpath, './') . '/';
        return true;
    }
    /**
     * 检测上传目录(OSS上传时支持自动创建目录，直接返回)
     * @param  string $savepath 上传目录
     * @return boolean          检测结果，true-通过，false-失败
     */
    public function checkSavePath($savepath){
        return true;
    }
    /**
     * 创建文件夹 (OSS上传时支持自动创建目录，直接返回)
     * @param  string $savepath 目录名称
     * @return boolean          true-创建成功，false-创建失败
     */
    public function mkdir($savepath){
        return true;
    }
    /**
     * 保存指定文件
     * @param  array   $file    保存的文件信息
     * @param  boolean $replace 同名文件是否覆盖
     * @return boolean          保存状态，true-成功，false-失败
     */
    public function save(&$file,$replace=true) {
        // var_dump($file);die;
        // 根据类型判断该走哪个通道上传（一般的情况下不需要，但是这里有视频不一样）        
        $key = "upload/".$file['savepath'] . $file['savename'];
        $content = fopen( $file['tmp_name'],'r');
        $video_type = array(
            "video/mp4",
            "video/ogg",
            "video/webm"
        );
        try{
            if( in_array($file['type'],$video_type) )
            {
                #走视频上传通道
                $key = "upload/ad/video/".$file['savename'];
                $result = OssClient::uploadVideo($key,$file['tmp_name']);            
            }
            $result = OssClient::upload($key,$file['tmp_name']);
            $file['urlpath'] = $file['url'] = $result;
        }catch(\Exception $e)
        {
            return false;
        }
        
        return $result;

        $size = $file['size'];
        $save = $this->client->putObject(array(
            'Bucket'    => $this->config['Bucket'],
            'Key'       => $key,
            'Content'   => $content,
            'ContentLength'=> $size,
            ));
        if ($save) {
            $file['urlpath'] = $file['url']='http://'.$this->config['domain'].'/'.$key;
            return $this->config['domain'].$key;
        }else{
            return false;
        }
    }
    public function getError(){
        return $this->client->errorStr;
    }

}